{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Episode 6: Domain, custom actions and slots\n",
    "\n",
    "In this episode we covered the reamining components of the Rasa dialogue management models - domain, custom actions and slots."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Define a domain\n",
    "\n",
    "Update the domain with all intents, entities, actions and template for the medicare locator assistant:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "domain_yml = \"\"\"\n",
    "intents:\n",
    "  - greet\n",
    "  - goodbye\n",
    "  - affirm\n",
    "  - deny\n",
    "  - mood_great\n",
    "  - mood_unhappy\n",
    "  - inform\n",
    "  - search_provider\n",
    "  - thanks\n",
    "\n",
    "entities:\n",
    "  - location\n",
    "  - facility_type\n",
    "\n",
    "slots:\n",
    "  location:\n",
    "    type: text\n",
    "  facility_type:\n",
    "    type: text\n",
    "  address:\n",
    "    type: unfeaturized\n",
    "\n",
    "actions:\n",
    "- utter_greet\n",
    "- utter_cheer_up\n",
    "- utter_did_that_help\n",
    "- utter_happy\n",
    "- utter_goodbye\n",
    "- utter_how_can_i_help\n",
    "- utter_ask_location\n",
    "- action_facility_search\n",
    "\n",
    "templates:\n",
    "  utter_greet:\n",
    "  - text: \"Hey! How are you?\"\n",
    "\n",
    "  utter_cheer_up:\n",
    "  - text: \"Here is something to cheer you up:\"\n",
    "    image: \"https://i.imgur.com/nGF1K8f.jpg\"\n",
    "\n",
    "  utter_did_that_help:\n",
    "  - text: \"Did that help you?\"\n",
    "\n",
    "  utter_happy:\n",
    "  - text: \"Great carry on!\"\n",
    "\n",
    "  utter_goodbye:\n",
    "  - text: \"Bye\"\n",
    "\n",
    "  utter_how_can_i_help:\n",
    "  - text: \"Hello. I am a medicare locator. I can help you find nearset hospital. How can I help?\"\n",
    "  - text: \"Hi. My name is medicare locator. How can I help you today?\"\n",
    "\n",
    "  utter_ask_location:\n",
    "  - text: \"Can you provide your location please?\"\n",
    "  - text: \"To find the nearest {facility_type} I need your address.\"\n",
    "\"\"\"\n",
    "\n",
    "%store domain_yml > domain.yml"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Implement the custom action\n",
    "\n",
    "In the episode we also implemented a simple custom actions which looks for a specified facility based on user's preferences and sets the returne address as a slot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "action_py = \"\"\"\n",
    "\n",
    "from typing import Any, Text, Dict, List\n",
    "\n",
    "from rasa_sdk import Action, Tracker\n",
    "from rasa_sdk.executor import CollectingDispatcher\n",
    "from rasa_sdk.events import SlotSet\n",
    "\n",
    "\n",
    "class ActionFacilitySearch(Action):\n",
    "\n",
    "    def name(self) -> Text:\n",
    "        return \"action_facility_search\"\n",
    "\n",
    "    def run(self, dispatcher: CollectingDispatcher,\n",
    "            tracker: Tracker,\n",
    "            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:\n",
    "\n",
    "        facility = tracker.get_slot(\"facility_type\")\n",
    "        address = \"300 Hyde St, San Francisco\"\n",
    "        dispatcher.utter_message(\"Here is the address of the {}:{}\".format(facility, address))\n",
    "\n",
    "        return [SlotSet(\"address\", address)]\n",
    "        \n",
    "\"\"\"        \n",
    "%store action_py > actions.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Updating the stories\n",
    "Training stories should be updated to reflect the SlotSet events which are executed in specific custom ations:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stories_md = \"\"\"\n",
    "## hospital search happy path\n",
    "* greet\n",
    "  - utter_how_can_i_help\n",
    "* search_provider{\"facility_type\":\"hospital\", \"location\": \"San Francisco\"}\n",
    "  - action_facility_search\n",
    "  - slot{\"address\":\"300 Hyde St, San Francisco\"}\n",
    "* thanks\n",
    "  - utter_goodbye\n",
    "\n",
    "## hospital search + location\n",
    "* greet\n",
    "  - utter_how_can_i_help\n",
    "* search_provider{\"facility_type\":\"hospital\"}\n",
    "  - utter_ask_location\n",
    "* inform{\"location\":\"San Francisco\"}\n",
    "  - action_facility_search\n",
    "  - slot{\"address\":\"300 Hyde St, San Francisco\"}\n",
    "* thanks\n",
    "  - utter_goodbye\n",
    "\n",
    "## happy path\n",
    "* greet\n",
    "  - utter_greet\n",
    "* mood_great\n",
    "  - utter_happy\n",
    "\n",
    "## sad path 1\n",
    "* greet\n",
    "  - utter_greet\n",
    "* mood_unhappy\n",
    "  - utter_cheer_up\n",
    "  - utter_did_that_help\n",
    "* affirm\n",
    "  - utter_happy\n",
    "\n",
    "## sad path 2\n",
    "* greet\n",
    "  - utter_greet\n",
    "* mood_unhappy\n",
    "  - utter_cheer_up\n",
    "  - utter_did_that_help\n",
    "* deny\n",
    "  - utter_goodbye\n",
    "\n",
    "## say goodbye\n",
    "* goodbye\n",
    "  - utter_goodbye\n",
    "\n",
    "\"\"\"        \n",
    "%store stories_md > data/stories.md\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Train the model\n",
    "With training data, domain, custom actions in place we can train and test our assistant to see how it works with NLU and dialogue management models in place."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nest_asyncio\n",
    "\n",
    "nest_asyncio.apply()\n",
    "print(\"Event loop ready.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import rasa\n",
    "\n",
    "# define the project files\n",
    "config = \"config.yml\"\n",
    "training_files = \"data/\"\n",
    "domain = \"domain.yml\"\n",
    "output = \"models/\"\n",
    "\n",
    "#train the example assistant\n",
    "model_path = rasa.train(domain, config, [training_files], output)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Test the assistant:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from rasa.jupyter import chat\n",
    "chat(model_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
